//
//  LZBaseTabBarViewController.m
//  ShellWallert
//
//  Created by lvzhao on 2019/5/13.
//  Copyright © 2019 吕VV. All rights reserved.
//

#import "LZBaseTabBarViewController.h"
#import "LZBaseNavigationController.h"


#import "ShellHomeViewController.h"
#import "ShellGrabOrderViewController.h"
#import "ShellCoinViewController.h"
#import "ShellWallertViewController.h"
#import "ShellMineViewController.h"



@interface LZBaseTabBarViewController ()<UITabBarControllerDelegate>

@end

@implementation LZBaseTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBar.translucent = NO;
    
    [[UITabBar appearance] setBarTintColor:UIColorHex(0x0D3354)];
    //字体大小，颜色（未被选中时）
    [[UITabBarItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:UIColorHex(0x6699BF),
                                                      NSForegroundColorAttributeName,k_textFont(11),NSFontAttributeName,nil]forState:UIControlStateNormal];
    
    
    //字体大小，颜色（选中时）
    [[UITabBarItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:COLOR_TEXT,
                                                      NSForegroundColorAttributeName,k_textBoldFont(11),NSFontAttributeName,nil]forState:UIControlStateSelected];
    
    if(K_Device_Is_iPhoneX){
        //字体与图片到的间隔
        [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, 1)];
    } else {
        //字体与图片到的间隔
        [[UITabBarItem appearance] setTitlePositionAdjustment:UIOffsetMake(0, -2)];
    }
    
    self.delegate = self;
    //添加子控制器
    [self addChilds];
    
}

//添加子视图
- (void)addChilds{
    
    //矿池
    ShellHomeViewController *homeVC = [[ShellHomeViewController alloc]init];
    LZBaseNavigationController *homeNav = [self setupViewController:homeVC title:LZLocalizedString(@"矿池") imageName:@"ico_home_unselect" selectImageName:@"ico_home_select"];
    
    
    //抢单
    ShellGrabOrderViewController *orderVC = [[ShellGrabOrderViewController alloc]init];
    LZBaseNavigationController *orderNav = [self setupViewController:orderVC title:LZLocalizedString(@"接单") imageName:@"ico_order_unselect" selectImageName:@"ico_order_select"];


    //shellcoin
    ShellCoinViewController *coinVC = [[ShellCoinViewController alloc]init];
    LZBaseNavigationController *coinNav = [self setupViewController:coinVC title:LZLocalizedString(@"Shell Coin") imageName:@"icon_shellcoin_unselect" selectImageName:@"icon_shellcoin_select"];

    
    //钱包
    ShellWallertViewController *wallertVC = [[ShellWallertViewController alloc]init];
    LZBaseNavigationController *wallertNav = [self setupViewController:wallertVC title:LZLocalizedString(@"钱包") imageName:@"ico_wallet_unselect" selectImageName:@"ico_wallet_select"];

    //我的
    ShellMineViewController *mineVC = [[ShellMineViewController alloc]init];
    LZBaseNavigationController *mineNav = [self setupViewController:mineVC title:LZLocalizedString(@"我的") imageName:@"ico_mine_unselect" selectImageName:@"ico_mine_select"];
    
    self.viewControllers = @[homeNav,orderNav,coinNav,wallertNav,mineNav];
    
}

#pragma mark - tabbar页面
- (LZBaseNavigationController *)setupViewController:(UIViewController *)viewController
                                          title:(NSString *)title
                                      imageName:(NSString *)imageName
                                selectImageName:(NSString *)selectImageName{
    
    LZBaseNavigationController * nav = [[LZBaseNavigationController alloc]initWithRootViewController:viewController];
    viewController.tabBarItem.title = title;
    viewController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.title = title;
    UIImage * selectImage = [UIImage imageNamed:selectImageName];
    //防止渲染Image.
    selectImage = [selectImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = selectImage;
    return nav;
    
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    if (tabBarController.selectedIndex > 0 && k_userToken.length == 0){
        tabBarController.selectedIndex = 0;
        ShellLoginViewController *login = [[ShellLoginViewController alloc] init];
        LZBaseNavigationController *nav = [[LZBaseNavigationController alloc] initWithRootViewController:login];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [LZTool.currentViewController.navigationController presentViewController:nav animated:YES completion:nil];
    }
    NSLog(@"didSelectViewController");
}


//改变tabbar高度
//- (void)viewWillLayoutSubviews{
//
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        CGRect tabFrame = self.tabBar.frame;
//        tabFrame.size.height = K_BARHEIGHT;
//        tabFrame.origin.y = self.view.frame.size.height - K_BARHEIGHT;
//        self.tabBar.frame = tabFrame;
//    });
//}



@end
